#include <iostream>
#include <boost/program_options.hpp>
#include <string>
#include <math.h>
#include "get_arg.h"


// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp

#define DEBUG_LEVEL 6
#include <debug_func.h>


// my functions
#include <print_error.h>
#include <program_options_helper.h>

using std::string;
using std::cerr;
using namespace program_options_helper;
namespace po = boost::program_options_lg;


//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{
	char input_format;

//	po::options_description mandatory_args("Mandatory Arguments");
//	mandatory_args.add_options()
//	("help,h",						"Display this message.");


	args.optional_args.add_options()
	("input_format,i", po::value<char>(&input_format)->default_value('p')->set_name("FORMAT"),
									"Input data can be in "
									"binary ('b'), pairs ('p'), "
									"square matrix ('s') or lower triangular ('l') "
									"format.")
	("weighting,w", po::value<float>(&args.power)->default_value(2.0f)->set_name("NUMBER"),
									"Squared differences between predicted and observed distances "
									"are down-weighted by the observed distance to the NUMBERth power. \01"
									"If > 2, the weighting of errors will fall off sharply "
									"for greater distances.")
	("iterations,I", po::value<unsigned>(&args.cnt_iterative_fitch_passes)
						   		->default_value(20)->set_name("ITER"),
									"The data will be randomly reordered ITER times to "
									"search for the global optimum.")
	("header,H",					"Add a header line (prefixed with '>') ");

	args.add_std_options();
	po::options_description all_args("");
	all_args.add(args.optional_args)
//							.add(mandatory_args)
							;

	// parse command line
	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(all_args).run(), vm);
	po::notify(vm);

	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.0\n"
			"\tHistory: \n\n"
			"\tv 1.0\1"
			"Initial version\n"
//			"\tv 1.1\1"
//			"01-02-2005\1"
//			"Split programme so that vanilla version does not do anything with matrices.\n"
			);
		return false;
	}
	if (vm.count("help"))
	{
		string exe_description =
				"Takes distances from STDIN "
				"and reconstructs the phylogenetic tree using a "
				"modified version of contempraneous fitch (Kitsch) based on "
				"PHYLIP code\1"
				"[Felsenstein, J. 1989 http://evolution.genetics.washington.edu/phylip]\1"
				"that takes null relationships into account.\n"
				"The resulting tree is printed to STDOUT\n\n";

		print_usage(cerr, string(*argv), "[OPTIONS] < MATRIX_FILE",
							all_args, exe_description, 80);
		return false;
	}

	args.header = vm.count("header");
	args.cnt_iterative_fitch_passes =	std::min(args.cnt_iterative_fitch_passes, 200U);


	if (args.power < 0.0)
		args.power= 2.0;
	select_matrix_file_format(input_format, args.input_format, "input format");


	args.open_err_log();
	return true;
}






